<?php

namespace app\admin\controller\cha;

use app\admin\controller\BaseController;
use app\common\model\ExpressAttr;
use app\common\service\logistics\ChannelService;
use think\Db;
use think\facade\Validate;

/**
 * 物流属性管理
 * Class ExpressAttrController
 * @package app\admin\controller\cha
 */
class ExpressAttrController extends BaseController
{
    /**
     * 物流属性列表
     * @return mixed
     */
    public function index()
    {
        //不是搜索调用，直接展示页面
        if(!$this->request->isAjax()) return $this->fetch('index');
        
        //搜索查询，展示列表
        $attrName = $this->request->post('attr_name','','trim');
        $attrCode = $this->request->post('attr_code','','trim');
        $createTime = $this->request->post('create_time','','trim'); //时间段，~分割

        //数据表模型
        $expressAttrModel = Db::name('express_attr')->order("create_time desc");
        //名称
        if($attrName)
        {
            $expressAttrModel->where('attr_name', 'like', '%' . $attrName . '%');
        }
        //编码
        if($attrCode)
        {
            $expressAttrModel->where('attr_code', $attrCode);
        }
        //时间
        if($createTime)
        {
            $createTime = range_time($createTime); //获取时间数组
            $expressAttrModel->where('create_time', 'between', [$createTime[0], $createTime[1]]);
        }

        $expressAttrs = $expressAttrModel->order('attr_id desc')->paginate();
        $list = $expressAttrs->getCollection()->toArray();
        $page = $expressAttrs->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch('lists');
    }

    /**
     * 更新物流属性
     */
    public function save()
    {
        if($this->request->isPost())
        {
            $data = $this->request->post();
            // 普通验证
            $validate = Validate::make(
                [
                    'attr_name'  => 'require',
                    'attr_code'  => 'require',
                ],
                [
                    'attr_name.require' => '快递名称必填',
                    'attr_code.require' => '属性编码必填',
                ]
            );
            if(!$validate->batch()->check($data)) $this->error(join(', ', $validate->getError()));
        }
        else
        {
            //只修改状态
            $validate = Validate::make([
                'attr_id'  => 'require',
            ],[
                'attr_id.require' => '非法请求',
            ]);
            $data = $this->request->only(['attr_id', 'status'], 'get');
            if(!$validate->batch()->check($data))$this->error(join(', ', $validate->getError()));
        }
        
        ChannelService::getInstance()->addExpressAttr($data)
            ? $this->success('操作成功')
            : $this->error('操作失败');
    }

    /**
     * 新增物流属性
     */
    public function add()
    {
        $attrID = $this->request->request("attr_id");
        if(!empty($attrID)) $this->assign("express_attr", ExpressAttr::get($attrID));
        return $this->fetch("add");
    }

    /**
     * 删除属性
     */
    public function delete()
    {
        $attrID = $this->request->request("ids");
        if(empty($attrID)) $this->error('参数错误');
        ExpressAttr::destroy($attrID);
        $this->success('删除成功');
    }
}
